package priv.stones.common.utils.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * @Author: Stones
 * @Description:全局配置
 * @Date: 2017/10/26 16:05
 */
public final class GlobalConfig {

    private static Logger logger = LoggerFactory.getLogger(GlobalConfig.class);

    static {
        try {
            GlobalConfig.webSite = representationWebSite();
        } catch (IOException e) {
            logger.error(e.getMessage());
        } catch (ClassNotFoundException e) {
            logger.error(e.getMessage());
        }
    }

    /**
     * 网站配置数据
     */
    public static WebSite webSite;

    /**
     * 设置网站配置数据
     *
     * @param webSite
     * @throws IOException
     */
    public static void setWebSite(WebSite webSite) throws IOException {
        GlobalConfig.webSite = webSite;
        File file = new File(System.getProperty("user.dir") + File.separator + "websiteConfig.ser");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(GlobalConfig.webSite);
        oos.close();
    }

    /**
     * 重显网站配置数据
     *
     * @return
     */
    private static WebSite representationWebSite() throws IOException, ClassNotFoundException {
        File file = new File(System.getProperty("user.dir") + File.separator + "websiteConfig.ser");
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            WebSite obj = (WebSite) ois.readObject();
            ois.close();
            fis.close();
            return obj;
        }
        return new WebSite();
    }

}
